Skip to content

MkText

Show source on GitHub

Class for any Markup text.

Description

All classes inheriting from MkNode can get converted to this Type.

Example: Regular

Jinja

{{ "Some text" | MkText }}

Python

MkText('Some text')

Some text

Some text
<p>Some text</p>

Bases: MkNode

__init__

__init__(text: str | MkNode | None = '', *, render_jinja: bool = False, **kwargs: Any)

Parameters:

Name Type Description Default
text str | MkNode | None

Markup text

''
render_jinja bool

Whether text should get rendered by this node

False
kwargs Any

Keyword arguments passed to parent

{}

from_url classmethod

from_url(url: str) -> Self | None

Parameters:

Name Type Description Default
url str

URL to get markdown from.

required
Name Children Inherits
MkInclude
mknodes.templatenodes.mkinclude
Node to include the text of other Markdown files / MkNodes.
    MkChangelog
    mknodes.templatenodes.mkchangelog
    Node for a git-based changelog (created by git-changelog).
      MkLicense
      mknodes.templatenodes.mklicense
      Node to show a license.
        MkLlm
        mknodes.templatenodes.mkllm
        Node for LLM-based text generation.
          Name Children Inherits
          MkNode
          mknodes.basenodes.mknode
          Base class for everything which can be expressed as Markup.
          graph TD
            94721309096560["mktext.MkText"]
            94721308848336["mknode.MkNode"]
            94721311766592["node.Node"]
            140564252373184["builtins.object"]
            94721308848336 --> 94721309096560
            94721311766592 --> 94721308848336
            140564252373184 --> 94721311766592
          /home/runner/work/mknodes/mknodes/mknodes/basenodes/mktext/metadata.toml
          [metadata]
          icon = "mdi:text"
          name = "MkText"
          group = "base"
          
          [examples.regular]
          title = "Regular"
          jinja = """
          {{ "Some text" | MkText }}
          """
          
          # [examples.from_url]
          # title = "From URL"
          # jinja = """
          # {{ mk.MkText.from_url("https://raw.githubusercontent.com/phil65/mknodes/main/README.md") }}
          # """
          
          [output.markdown]
          template = """
          {{ node.text }}
          """
          
          mknodes.basenodes.mktext.MkText
          class MkText(mknode.MkNode):
              """Class for any Markup text.
          
              All classes inheriting from MkNode can get converted to this Type.
              """
          
              ICON = "material/text"
          
              def __init__(
                  self,
                  text: str | mknode.MkNode | None = "",
                  *,
                  render_jinja: bool = False,
                  **kwargs: Any,
              ):
                  """Constructor.
          
                  Args:
                      text: Markup text
                      render_jinja: Whether text should get rendered by this node
                      kwargs: Keyword arguments passed to parent
                  """
                  super().__init__(**kwargs)
                  self._text = str(text or "")
                  self.render_jinja = render_jinja
          
              def __getitem__(self, section_name: str) -> Self | None:
                  markdown = self._to_markdown()
                  section_text = mdfilters.extract_header_section(markdown, section_name)
                  return None if section_text is None else type(self)(section_text)
          
              @property
              def text(self) -> str:
                  if not self.render_jinja:
                      return self._text
                  return self.env.render_string(self._text)
          
              @text.setter
              def text(self, value):
                  self._text = value
          
              def _to_markdown(self) -> str:
                  return self.text
          
              @property
              def children(self):
                  if not self.render_jinja:
                      return []
                  self.env.render_string(self._text, variables=self.variables)
                  return self.env.rendered_children
          
              @children.setter
              def children(self, val):
                  pass
          
              @classmethod
              def from_url(cls, url: str) -> Self | None:
                  """Build a MkText node on a remote markup file.
          
                  If the URL contains a "#" (http://.../markdown.md#section),
                  it will try to extract the given section.
          
                  All fsspec protocols are supported as URL.
          
                  Args:
                      url: URL to get markdown from.
                  """
                  url, *section = url.split("#")
                  text = pathhelpers.load_file_cached(url)
                  if not section:
                      return cls(text)
          
                  extracted = mdfilters.extract_header_section(text, section[0])
                  return cls(extracted) if extracted is not None else None